package org.NooLab.glue.net.http.rest.resources;


import org.restlet.data.Form;
import org.restlet.data.Parameter;
import org.restlet.engine.http.header.HeaderConstants;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.Put;




public class DataRelayResource extends BaseResource{
	
	
	
	
	public DataRelayResource(){
		 
	}
	
	@Get
	public String DataHandlerGet(){
		
		out.print(4, "handling data transmission...") ;
		 
		String getmsg = "NooLabGlue Server Component 0.02.01 \n\n"
	 					+ "authentication is required!  \n\n"
	 					+ "usage:\n"
	 					+ "put  	->  for opening a transaction, or registering \n"
	 					+ "post		->  for transferring data, polls etc. \n"
	 					+ "get 		->  for state requests about transactions \n";
		
		// get param from request
		// String fooParam = getQuery().getFirstValue('foo');  
        // getQuery().getValues("userId")
		// getRequest().getAttributes().get('page');
		
		return getmsg;
	}
 
	  
	
	@Post 
	public String DataHandlerPost(Representation entity){   
		
		String msgText="", role="" ;
		MessageSupply msgSupply;
		
		
		role = this.getRegistrationTarget(); // SOURCE, RECEPTOR
		msgSupply = prepareMessageRouting();

		try {
			
								out.print(5, "Restlet-Server (1), DataHandlerPost(A) -> ID : "+this.toString() );
			
			msgText = entity.getText();
		
			
		} catch (Exception e) {
			e.printStackTrace();
		}

		// this will invoke "msgSupply.serveToMessageBoard()", which is simply an notification of the
		// observing messageBoard;
		// yet, it is itself starting an object which serves an observer for the returned messages
		serveAndGetReturn( msgSupply, msgText);
		
		getResponse().commit();
		
		// we need a confirmation to be returned ! MISSING SO FAR
								out.print(5, "returned string in DataHandlerPost() : \n"+ regResult) ;
		
		return regResult;
		
		// String getmsg = "response by '"+this.toString()+"', Confirmation for post id=21323435, response from server" ;
		  
	}
	
	
	
	protected void addResponseHeader(String name, String value) {
		
	    Form responseHeaders = (Form)getResponse().getAttributes().get(HeaderConstants.ATTRIBUTE_HEADERS);
	    if (responseHeaders == null) {
	        responseHeaders = new Form();
	        getResponse().getAttributes().put(HeaderConstants.ATTRIBUTE_HEADERS, responseHeaders);
	    }
	    responseHeaders.add(new Parameter(name, value));
	}
	

	 /*
	  * getResponse().setEntity() method from acceptRepresentation() will accept the new FileRepresentation
	  * 
	  */

}
